/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package auctions;

import java.io.File;
import java.util.ArrayList;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.*;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.*;

/**
 *
 * @author hangsu
 */
public class ScheduleAuctionsToXML {

	private Document _doc;
	private ArrayList<AuctionResult> _results;
	private ArrayList<BidderResult> _bresults;
	
	public ScheduleAuctionsToXML(ArrayList<AuctionResult> results, ArrayList<BidderResult> bresults) {
            _results = results;
            _bresults = bresults;
		
            try {
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		DocumentBuilder parser = factory.newDocumentBuilder();
		Document doc = parser.newDocument();
		_doc = doc;
            } catch (Exception e) {
                System.out.println(e.getMessage());
            }
	}
	
	public void setResults(ArrayList<AuctionResult> results, ArrayList<BidderResult> bresults) {
            _results = results;
            _bresults = bresults;
	}
	
	public void generateXML(String filename) {
            //Print the all results to an XML file
            Element root = _doc.createElement("Results");
            _doc.appendChild(root);
            Element auctionResults = _doc.createElement("AuctionResults");
            root.appendChild(auctionResults);
            for (AuctionResult r : _results) {
                Element auction = _doc.createElement("Auction");
                auction.setAttribute("number", Integer.toString(r.getIdx()));
                auction.setAttribute("winningBidder", 
                        Integer.toString(r.getFinalResult().currentWinner));
                auction.setAttribute("winningBid", 
                        Double.toString(r.getFinalResult().winnerBid));
                auction.setAttribute("price", 
                        Double.toString(r.getFinalResult().currentPrice));
                auctionResults.appendChild(auction);

                ArrayList<Double> bidValues = null;
                for (int i = 0; i<r.getRound(); i++) {
                    bidValues = r.getBidsOfRound(i);
                    Element round = _doc.createElement("Round");
                    round.setAttribute("idx", Integer.toString(i));
                    for (int j=0; j<bidValues.size(); j++) {
                        Element bid = _doc.createElement("Bid");
                        bid.setAttribute("bidder", Integer.toString(j));
                        bid.setAttribute("value", Double.toString(bidValues.get(j).doubleValue()));
                        round.appendChild(bid);
                    }
                    auction.appendChild(round);
                }
            }
		
            Element bidderResults = _doc.createElement("BidderResults");
            root.appendChild(bidderResults);

            for (int i = 0; i< _bresults.size(); i++) {
                BidderResult br = _bresults.get(i);
                Element bidder = _doc.createElement("Bidder");
                bidder.setAttribute("Bidder", Integer.toString(br.getIdx()));
                bidder.setAttribute("Revenue", Double.toString(br.getUtil()));
                bidder.setAttribute("Cost", Double.toString(br.getCost()));
                bidder.setAttribute("Profit", Double.toString(br.getProfit()));
                bidderResults.appendChild(bidder);
                for(int j =0; j<br.getItemsWon().length; j++)
                {
                    if(br.getItemsWon()[j]) {
                        Element item = _doc.createElement("Item");
                        item.setAttribute("ItemNumber", Integer.toString(j));
                        bidder.appendChild(item);
                    }
                }
            }

            this.writeXmlFile(_doc, filename);
	}
	
	private void writeXmlFile(Document doc, String filename) {
	    try {
	        // Prepare the DOM document for writing
	        Source source = new DOMSource(doc);

	        // Prepare the output file
	        File file = new File(filename);
	        Result result = new StreamResult(file);

	        // Write the DOM document to the file
	        Transformer xformer = TransformerFactory.newInstance().newTransformer();
	        xformer.transform(source, result);
	    } catch (Exception e) {
	    	System.out.println(e.getMessage());
	    }
	}

}
